package com.utils.thread;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class LineLogger {
	private static final Logger logger = LoggerFactory.getLogger(LineLogger.class);

	private static ExecutorService threadPool;
	private static final int nThread = 1;

	static {
		if (threadPool == null)
			threadPool = new ThreadPoolExecutor(nThread, nThread, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024));
	}

	// private static

	/**
	 * 通用log打印
	 * 
	 * @param url
	 * @param useThread
	 *            ，是否用多线程
	 */

	public static void log(final String url, boolean useThread) {
		if (useThread) {
			try {
				if (threadPool == null)
					threadPool = new ThreadPoolExecutor(nThread, nThread, 0L, TimeUnit.MILLISECONDS,
							new LinkedBlockingQueue<Runnable>(1024));

				threadPool.submit(new Runnable() {
					@Override
					public void run() {

						// logger.info((request != null ?
						// request.getRequestURI() + "?" +
						// request.getQueryString() :
						// "null")
						// + "\t"
						// + recContext.toString() + "\t" + result.toStr());
						logger.info(url);
					}
				});
			} catch (Exception e) {
				logger.error("threadPool is null? =" + (threadPool == null) + " log has error", e);
			}
		} else {
			logger.info(url);
		}

	}

	public static void close() {
		//logger.info(Tidy.getCurrentClassAndMethodNames(Tidy.CUR_METHOD_STACK) + " is shutting down...");
		if (threadPool != null)
		threadPool.shutdown();
	}

}
